{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "07311335",
   "metadata": {},
   "source": [
    "# Парсер значений, разделённых запятой"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77e49a3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import LLMChain\n",
    "from langchain.chat_models import GigaChat\n",
    "from langchain.output_parsers import CommaSeparatedListOutputParser\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.schema import HumanMessage\n",
    "\n",
    "model = GigaChat(credentials=...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ace93488",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_parser = CommaSeparatedListOutputParser()\n",
    "\n",
    "format_instructions = output_parser.get_format_instructions()\n",
    "prompt = PromptTemplate(\n",
    "    template=\"Придумай несколько {subject}, {format_instructions}\",\n",
    "    input_variables=[\"subject\"],\n",
    "    partial_variables={\"format_instructions\": format_instructions},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9240a3ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Барбос',\n",
       " 'Бимка',\n",
       " 'Бобик',\n",
       " 'Дружок',\n",
       " 'Джек',\n",
       " 'Джой',\n",
       " 'Жучка',\n",
       " 'Лайма',\n",
       " 'Лаки',\n",
       " 'Маркиз',\n",
       " 'Муха',\n",
       " 'Найда',\n",
       " 'Пушок',\n",
       " 'Рекс',\n",
       " 'Рыжик',\n",
       " 'Снежок',\n",
       " 'Трезор',\n",
       " 'Тимка',\n",
       " 'Томка',\n",
       " 'Чипс.']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output = model([HumanMessage(content=prompt.format(subject=\"кличек для собаки\"))])\n",
    "output_parser.parse(output.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "9174fe3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['$1000', 'Billionaire', 'Ice Cream Mania', 'Wine Country', 'Sugar Plum Fairy']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output = model(\n",
    "    [HumanMessage(content=prompt.format(subject=\"названий для коктейля за $1000\"))]\n",
    ")\n",
    "output_parser.parse(output.content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
